import Cookies from 'js-cookie'

const tokenKey = 'user-info-token-base';
const userInfo = 'user-info-customer-base';

export function getToken() {
  return Cookies.get(tokenKey)
}

export function setToken(token) {
  return Cookies.set(tokenKey, token)
}

export function removeToken() {
  return Cookies.remove(tokenKey)
}

export function write(key, value) {
  if (value) {
    value = JSON.stringify(value);
  }
  localStorage.setItem(key, value);
}

export function read(key) {
  let value = localStorage.getItem(key);
  if (value && value != "undefined" && value != "null") {
    return JSON.parse(value);
  }
  return null
}

export function remove(key) {
  localStorage.removeItem(key);
}

export function clear() {
  localStorage.clear();
}

export function writeSession(key, value) {
  if (value) {
    value = JSON.stringify(value);
  }
  sessionStorage.setItem(key, value);
}

export function setUserInfo(value) {
  write(userInfo, value);
}

export function getUserInfo() {
  return read(userInfo);
}

export function readSession(key) {
  let value = sessionStorage.getItem(key);
  if (value && value != "undefined" && value != "null") {
    return JSON.parse(value);
  }
  return null
}

export function removeSession(key) {
  sessionStorage.removeItem(key);
}

export function clearSession() {
  sessionStorage.clear();
}
